package com.zhihuichongcheng.application.ui.coordination.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.smartlib.cmnObject.recycleView.OnItemOnclick;
import com.smartlib.cmnObject.util.DateUtil;
import com.smartlib.cmnObject.util.FileUtil;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.bean.coordination.CoordinationFileBean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 3water on 2016/10/29.
 */

public class CoordinationAdapter extends RecyclerView.Adapter<CoordinationAdapter.ViewHold> {
    private Context mContext;
    private OnItemOnclick<CoordinationFileBean> onItemOnclick;
    private List<CoordinationFileBean> mListData = new ArrayList<>();
    public CoordinationAdapter(Context context)
    {
        this.mContext = context;
    }
    public void setData(List<CoordinationFileBean> list)
    {
        this.mListData.addAll(list);
    }
    @Override
    public ViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.listadapter_coordination,null);
        return new ViewHold(view);
    }

    public void setOnItemOnclick(OnItemOnclick<CoordinationFileBean> onItemOnclick)
    {
        this.onItemOnclick = onItemOnclick;
    }
    @Override
    public void onBindViewHolder(ViewHold holder, final int position) {
       final CoordinationFileBean bean = mListData.get(position);
        holder.mTvName.setText(bean.getFileName());
      final int type = bean.getFileType();
        if(type==1)
        {//是文件
            Date date = DateUtil.parseStringToDate(bean.getCreateTime(),"yyyy年MM月dd日 HH:mm:ss");
            holder.mTvCreateTime.setText(DateUtil.format(date));
            holder.mTvFileSize.setText(FileUtil.getFormatSize(bean.getFileSize()));
            holder.mTvFileSize.setVisibility(View.VISIBLE);
            holder.mTvCreateTime.setVisibility(View.VISIBLE);
            String contentType = bean.getContentType();
            if(contentType.equals("pdf"))
            {
                holder.mImAvatar.setBackgroundResource(R.drawable.ic_coordination_pdf);
            }
            else if(contentType.equals("doc"))
            {
                holder.mImAvatar.setBackgroundResource(R.drawable.ic_coordination_doc);
            }else if(contentType.equals("png")||contentType.equals("jpg"))
            {
                holder.mImAvatar.setBackgroundResource(R.drawable.ic_coordination_img);
            }else if(contentType.equals("numbers"))
            {
                holder.mImAvatar.setBackgroundResource(R.drawable.ic_coordination_xlcs);
            }else if(contentType.equals("mp4"))
            {
                holder.mImAvatar.setBackgroundResource(R.drawable.ic_coordination_video);
            }else if(contentType.equals("mp3"))
            {
                holder.mImAvatar.setBackgroundResource(R.drawable.ic_coordination_music);
            }else if(contentType.equals("xlsx"))
            {
                holder.mImAvatar.setBackgroundResource(R.drawable.ic_coordination_xlcs);
            }else if(contentType.equals("zip"))
            {
                holder.mImAvatar.setBackgroundResource(R.drawable.ic_coordination_zip);
            }else if(contentType.equals("ppt"))
            {
                holder.mImAvatar.setBackgroundResource(R.drawable.ic_coordination_ppt);
            }
            else
            {
                holder.mImAvatar.setBackgroundResource(R.drawable.ic_coordination_other);
            }
        }
        else
        {
            holder.mTvFileSize.setVisibility(View.GONE);
            holder.mTvCreateTime.setVisibility(View.GONE);
            holder.mImAvatar.setBackgroundResource(R.drawable.ic_coordination_folder);
//            Glide.with(mContext).load(R.drawable.ic_coordination_folder).into(holder.mImAvatar);
        }
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemOnclick!=null)
                {
                    onItemOnclick.onClick(bean,position);
                }


            }
        });
//        holder.mTvFileSize.setText(bean.getSize());
//        holder.mTvCreateTime.setText(bean.getTime());
    }

    @Override
    public int getItemCount() {
        return mListData.size();
    }

    public void clear() {
        mListData.clear();
    }

    class ViewHold extends RecyclerView.ViewHolder{
        private TextView mTvName,mTvCreateTime,mTvFileSize;
        private ImageView mImAvatar;
        private View view;
        public ViewHold(View itemView) {
            super(itemView);
            view = itemView;
            mTvName = (TextView)itemView.findViewById(R.id.tv_name);
            mTvCreateTime = (TextView)itemView.findViewById(R.id.tv_time);
            mTvFileSize = (TextView)itemView.findViewById(R.id.tv_file_size);
            mImAvatar = (ImageView)itemView.findViewById(R.id.im_avatar);
        }
    }
}
